package org.gd.spring.jdbc;

import org.gd.hellojaxb.interfaces.XmlObject;
import org.gd.rjdbc.annotation.TableField;
import org.gd.rjdbc.context.RJDBCContext;
import org.gd.rjdbc.exceptions.RJDBCException;
import org.springframework.jdbc.core.RowMapper;

import javax.xml.bind.annotation.*;
import java.sql.ResultSet;
import java.sql.SQLException;

@XmlRootElement(name = "Customer")
@XmlAccessorType(XmlAccessType.NONE)
@SuppressWarnings("unused")
public class Customer extends XmlObject implements RowMapper<Customer> {

    @XmlAttribute()
    @TableField(name = "CUST_ID", clazz = Integer.class)
    int customerId;
    @XmlAttribute()
    @TableField(name = "NAME")
    String name;
    @XmlAttribute()
    @TableField(name = "AGE", clazz = Integer.class)
    Integer age;

    public Customer() {
    }

    public Customer(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Customer(int customerId, String name, int age) {
        this.customerId = customerId;
        this.name = name;
        this.age = age;
    }

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Customer {" +
                "customerId=" + customerId +
                ", name='" + name + "'" +
                ", age=" + age +
                '}';
    }

    @Override
    public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
        try {
            return RJDBCContext.getInstance().getRSListener().parseOne(rs, Customer.class);
        } catch (RJDBCException e) {
            throw new SQLException(e);
        }
    }
}
